<?php
/*
Plugin Name: WP Admin Links
Plugin URI: http://helvensteijn.com/wordpress/wp-admin-links/
Description: Displays a drop-down list with admin links when logged in. But only those links that the user has access to.
Version: 1.1
Author: Colin Helvensteijn
Author URI: http://helvensteijn.com/
License: BSD license, see readme.txt for details
*/

/**
 * Template tag
 *
 * Wrapper for the WPAdminLinks class
 *
 * @param string $chooseText Text to display on page load
 * @param string $before Output before the drop-down list
 * @param string $after Output after the drop-down list
 * @param boolean Echo the drop-down list or return it
 * @return void
 **/
function wp_admin_links($chooseText = 'Admin links' ,$before = '', $after = '', $echo = true)
{
	$wpal = new WPAdminLinks($chooseText, $before, $after);
	$output = $wpal->create(false);
	unset($wpal);
	
	if($echo) echo $output;
	else return $output;
}

/**
 * WP Admin Links
 *
 * @package WPAdminLinks
 * @author Colin Helvensteijn
 **/
class WPAdminLinks
{
	/**
	 * Array of default pages 
	 *
	 * @var string
	 * @access private
	 **/
	var $defaultPages = array();
	/**
	 * Array of hooked pages
	 *
	 * @var array
	 * @access private
	 **/
	var $hookedPages = array();
	/**
	 * Array of default sections
	 *
	 * @var array
	 * @access private
	 **/
	var $defaultSections = array();
	/**
	 * Array of hooked sections
	 *
	 * @var string
	 * @access private
	 **/
	var $hookedSections = array();
	/**
	 * Text to display on page load
	 *
	 * @var string
	 **/
	var $chooseText;
	/**
	 * Output before the drop-down list
	 *
	 * @var string
	 * @access private
	 **/
	var $before;
	/**
	 * Output after the drop-down list
	 *
	 * @var string
	 * @access private
	 **/
	var $after;
		
	/**
	 * Cconstructor, sets everything in motion
	 *
	 * @param string $chooseText Text to display on page load
	 * @param string $before Output before the drop-down list
	 * @param string $after Output after the drop-down list
	 * @access public
	 **/
	function WPAdminLinks($chooseText = 'Admin links', $before = '', $after = '')
	{
		$this->chooseText = $chooseText;
		$this->before = $before;
		$this->after = $after;
		
		$this->_defaultSections();
		$this->_defaultPages();
		
		if(file_exists(dirname(__FILE__).'/custom.php'))
			include dirname(__FILE__).'/custom.php';
	}

	/**
	 * Creates the list
	 *
	 * @param boolean $echo Echo the dowp-down list or return it
	 * @return string The drop-down list, if $echo evaluates to false
	 * @access public
	 **/
	function create($echo = true)
	{
		$url = get_bloginfo('wpurl');
		$sections = array_merge($this->defaultSections, $this->hookedSections);
		$pages = array_merge_recursive($this->defaultPages, $this->hookedPages);
		
		if(is_user_logged_in() && count($pages) > 0)
		{
			$output = $this->before;
			$output .= '<select class="wp-admin-links" tabindex="95" '.
				'onchange="window.location = this.value">'.PHP_EOL;

			// choose text
			if($this->chooseText == '')
				$this->chooseText = __('Admin links');
			$output .= '<option disabled="disabled">'.$this->chooseText.'</option>'.PHP_EOL;

			// for all logged in users
			$output .= '<optgroup label="'.get_bloginfo('name').'">'.PHP_EOL;
			$output .= '<option value="'.$url.'/wp-admin/index.php">'.__('Dashboard').'</option>'.PHP_EOL;
			$output .= '<option value="'.$url.'/wp-admin/profile.php">'.__('Your profile').'</option>'.PHP_EOL;
			$output .= '<option value="'.$url.'/wp-login.php?action=logout">'.__('Logout').'</option>'.PHP_EOL;
			$output .= '</optgroup>'.PHP_EOL;
			
			foreach($sections as $section => $sectionTitle)
			{
				$sectionOpts = '';
				foreach($pages[$section] as $pageTitle => $link)
					$sectionOpts .= '<option value="'.$url.'/wp-admin/'.$link.'">'.$pageTitle.'</option>'.PHP_EOL;
				
				if($sectionOpts)
				{
					$output .= '<optgroup label="'.$sectionTitle.'">'.PHP_EOL;
					$output .= $sectionOpts;
					$output .= '</optgroup>'.PHP_EOL;
				}
			}

			$output .= '</select>'.$this->after;

			if($echo) echo $output;
			else return $output;
		}
	}
	
	/**
	 * Hooks a custom section into the list
	 *
	 * @param string $section Identifier for the section
	 * @param string $title Title of the section
	 * @return void
	 * @access public
	 **/
	function addSection($section, $title)
	{
		$this->hookedSections[$section] = $title;
	}

	/**
	 * Hooks a custom page into the list
	 *
	 * @param string $section Section identifier
	 * @param string $name Title of the page
	 * @param string $link Link of the page, relative to /wp-admin/
	 * @param int $capability Capability required to access the page
	 * @return void
	 * @access public
	 **/
	function addPage($section, $title, $link, $capability)
	{
		if(current_user_can($capability))
			$this->hookedPages[$section][$title] = $link;
	}
	
	/**
	 * Removes a page from the list
	 *
	 * @param string $section Section identifier
	 * @param string $title Title of the page
	 * @return void
	 * @access public
	 **/
	function removePage($section, $title)
	{
		if(isset($this->defaultPages[$section][$title]))
			unset($this->defaultPages[$section][$title]);
		elseif(isset($this->hookedPages[$section][$title]))
			unset($this->hookedPages[$section][$title]);
	}
	
	/**
	 * Creates the default sections
	 *
	 * @return void
	 * @access private
	 **/
	function _defaultSections()
	{
		$this->defaultSections = array(
			'write' => __('Write'),
			'manage' => __('Manage'),
			'comments' => __('Comments'),
			'blogroll' => __('Bogroll'),
			'presentation' => __('Presentaion'),
			'plugins' => __('Plugins'),
			'users' => __('Users'),
			'options' => __('Options')
		);
	}
	
	/**
	 * Creates the default pages
	 *
	 * @return void
	 * @author Colin Helvensteijn
	 **/
	function _defaultPages()
	{
		$this->defaultPages = array(
			'write' => array(),
			'manage' => array(),
			'comments' => array(),
			'blogroll' => array(),
			'presentation' => array(),
			'plugins' => array(),
			'users' => array(),
			'options' => array()
		);
		
		// write
		if(current_user_can('edit_posts'))
			$this->defaultPages['write'][__('Write Post')] = 'post-new.php';
		if(current_user_can('edit_pages'))
			$this->defaultPages['write'][__('Write Page')] = 'page-new.php';
		
		// manage
		if(current_user_can('edit_posts'))
			$this->defaultPages['manage'][__('Posts')] = 'edit.php';
		if(current_user_can('edit_pages'))
			$this->defaultPages['manage'][__('Pages')] = 'edit-pages.php';
		if(current_user_can('upload_files'))
			$this->defaultPages['manage'][__('Uploads')] = 'upload.php';
		if(current_user_can('manage_categories'))
			$this->defaultPages['manage'][__('Categories')] = 'categories.php';
		if(current_user_can('edit_files'))
			$this->defaultPages['manage'][__('Files')] = 'templates.php';
		if(current_user_can('import'))
		{
			$this->defaultPages['manage'][__('Import')] = 'import.php';
			$this->defaultPages['manage'][__('Export')] = 'export.php';
		}
		
		// comments
		if(current_user_can('edit_posts'))
			$this->defaultPages['comments'][__('Comments')] = 'edit-comments.php';
		if(current_user_can('moderate_comments'))
			$this->defaultPages['comments'][__('Awaiting Moderation')] = 'moderation.php';

		// blogroll
		if(current_user_can('manage_links'))
		{
			$this->defaultPages['blogroll'] = array(
				__('Manage Blogroll') => 'link-manager.php',
				__('Add Link') => 'link-add.php',
				__('Import Links') => 'link-import.php',
				__('Categories') => 'edit-link-categories.php'
			);
		}

		// presentation
		$presentationLinks = '';
		if(current_user_can('switch_themes'))
			$this->defaultPages['presentation'][__('Themes')] = 'themes.php';
		if(current_user_can('edit_themes'))
		{
			$this->defaultPages['presentation'][__('Widgets')] = 'widgets.php';
			$this->defaultPages['presentation'][__('Theme Editor')] = 'theme-editor.php';
		}
		
		// plugins
		if(current_user_can('activate_plugins'))
			$this->defaultPages['plugins'][__('Plugins')] = 'plugins.php';
		if(current_user_can('edit_plugins'))
			$this->defaultPages['plugins'][__('Plugin Editor')] = 'plugin-editor.php';
		
		// users
		if(current_user_can('edit_users'))
			$this->defaultPages['users'][__('Authors &amp; Users')] = 'users.php';
		
		// options
		if(current_user_can('manage_options'))
		{
			$this->defaultPages['options'] = array(
				__('General') => 'options-general.php',
				__('Writing') => 'options-writing.php',
				__('Reading') => 'options-reading.php',
				__('Discussion') => 'options-discussion.php',
				__('Privacy') => 'options-privacy.php',
				__('Permalinks') => 'options-permalink.php',
				__('Miscellaneous') => 'options-misc.php',
			);
		}
	}
}

/**
 * Sidebar widget stuff
 *
 * @return void
 * @author Colin Helvensteijn
 **/
function wp_admin_links_register_widget()
{
	if(function_exists('register_sidebar_widget'))
	{
		/**
		 * Widget
		 *
		 * @return void
		 **/
		function wp_admin_links_widget($args)
		{
			extract($args);
			$wpal = new WPAdminLinks();
			echo $before_widget;
			
			$wpal->create();
			
			echo $after_widget;
			unset($wpal);
		}
		
		/**
		 * Style for widget
		 *
		 * @return void
		 **/
		function wp_admin_links_widget_style()
		{
			?><style type="text/css">#wp-admin-links select{width:140px;margin:10px 0;}</style><?php
		}
	
    	register_sidebar_widget('WP Admin Links', 'wp_admin_links_widget', null, 'wp_admin_links_widget');
		if ( is_active_widget('wp_admin_links_widget') )
			add_action('wp_head', 'wp_admin_links_widget_style');
	}
}
add_action('init', 'wp_admin_links_register_widget');

?>